package com.ysd.lis.service.eq.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.eq.EqEquipment;
import com.ysd.lis.entity.eq.EqRunningRecord;
import com.ysd.lis.mapper.eq.EqRunningRecordMapper;
import com.ysd.lis.request.CommonPageDto;
import com.ysd.lis.request.eq.RunningRecordDto;
import com.ysd.lis.service.SysRuleSetService;
import com.ysd.lis.service.eq.EqRunningRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 设备管理-设备类型 服务实现类
 * </p>
 *
 * @author prs
 * @since 2024-08-26
 */
@Service
public class EqRunningRecordServiceImpl extends ServiceImpl<EqRunningRecordMapper, EqRunningRecord> implements EqRunningRecordService {

    @Autowired
    EqRunningRecordMapper eqRunningRecordMapper;

    @Override
    public Result findRunningRecordPageList(CommonPageDto pageParam) {
        Page<EqRunningRecord> page = new Page<>();
        page.setCurrent(pageParam.getPageIndex());
        page.setSize(pageParam.getPageSize());
        MPJLambdaWrapper<EqRunningRecord> lambdaQueryWrapper = new MPJLambdaWrapper<>();
        lambdaQueryWrapper.selectAll(EqRunningRecord.class)
                .selectAs("eq", EqEquipment::getEquiptName,EqRunningRecord::getEquiptName)
                .selectAs("eq",EqEquipment::getSpecification,EqRunningRecord::getSpecification)
                .leftJoin(EqEquipment.class,"eq",EqEquipment::getId,EqRunningRecord::getEquiptId)
                .eq(EqRunningRecord::getDelFlag, 0);
        if (StringUtils.isNotBlank(pageParam.getSeachValue())) {
            lambdaQueryWrapper.and(wrapper -> wrapper.like(EqRunningRecord::getRunningNo, pageParam.getSeachValue())
                    .or().like(EqRunningRecord::getEquiptCode, pageParam.getSeachValue()));
        }
        lambdaQueryWrapper.orderByDesc(EqRunningRecord::getCreateTime);
        Page<EqRunningRecord> planPage = eqRunningRecordMapper.selectPage(page, lambdaQueryWrapper);
        return Result.succ(1, "查询成功", planPage);
    }

    @Override
    public Result batchDeleteRunningRecordList(RunningRecordDto dto) {
        List<EqRunningRecord> eqRunningRecordList = dto.getEqRunningRecordList();
        List<String> ids = eqRunningRecordList.stream().map(EqRunningRecord::getId).collect(Collectors.toList());
        boolean b = this.removeByIds(ids);
        if(b){
            return Result.succ(1, "删除成功", null);
        }else {
            return Result.fail(400,"删除失败","");
        }
    }

    @Override
    public Result findRunningRecordById(EqRunningRecord runningRecord) {
        String id = runningRecord.getId();

        MPJLambdaWrapper<EqRunningRecord> lambdaQueryWrapper = new MPJLambdaWrapper<>();
        lambdaQueryWrapper.selectAll(EqRunningRecord.class)
                .selectAs("eq",EqEquipment::getEquiptName,EqRunningRecord::getEquiptName)
                .selectAs("eq",EqEquipment::getSpecification,EqRunningRecord::getSpecification)
                .leftJoin(EqEquipment.class,"eq",EqEquipment::getId,EqRunningRecord::getEquiptId)
                .eq(EqRunningRecord::getId,id)
                .eq(EqRunningRecord::getDelFlag, 0);
        EqRunningRecord eqRunningRecord = eqRunningRecordMapper.selectOne(lambdaQueryWrapper);


        return Result.succ(1, "查询成功", eqRunningRecord);

    }

    @Autowired
    SysRuleSetService sysRuleSetService;
    @Override
    public Result addRunningRecord(EqRunningRecord eqRunningRecord) {
        String a = sysRuleSetService.createRuleCodeList("eqRunningRecordNo", 1).get(0);
        eqRunningRecord.setRunningNo(a);
        boolean save = this.save(eqRunningRecord);
        if (save) {
            return Result.succ(1, "添加成功", eqRunningRecord);
        } else {
            return Result.fail("添加失败");
        }
    }

    @Override
    public Result updateRunningRecord(EqRunningRecord eqRunningRecord) {
        boolean save = this.updateById(eqRunningRecord);
        if (save) {
            return Result.succ(1, "编辑成功", eqRunningRecord);
        }else {
            return Result.fail("编辑失败");
        }
    }
}
